<?php
declare (strict_types = 1);

namespace app\controller;

use app\model\Admin as AdminModel;
use app\validate\AdminValidate;
use think\exception\ValidateException;
use think\facade\Validate;
use think\Request;

class Admin extends Base
{
    /**
     * 显示资源列表
     * get
     * @return \think\Response
     */
    public function index()
    {
        $page_config = [
            'list_rows'=> $this->page_size,
            'var_page' => 'page_num',
        ];
        if(!empty($this->request->param('query_name'))){
            $list = AdminModel::with('profile')->whereLike('account','%'.$this->request->param('query_name').'%')
                ->order('id', 'asc')
                ->paginate($page_config);
        }else {
            $list = AdminModel::with('profile')->order('id', 'asc')->paginate($page_config);
        }
        if($list->isEmpty()){
            return $this->create([],'管理员列表无数据',204);
        }
        return $this->create($list,'请求管理员列表数据成功');
    }

    /**
     * 保存新建的资源
     * post
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $param = $request->param();
        try {
            Validate(AdminValidate::class)->check($param);
        }catch (ValidateException $validateException){
            return $this->create([],$validateException->getError(),400);
        }
        $param['password'] = md5(sha1($param['password']));
        $id = AdminModel::create($param)->getData('id');
        if($id){
            return $this->create($id,'添加管理员成功');
        }else{
            return $this->create([],'添加管理员失败',400);
        }
    }

    /**
     * 显示指定的资源
     *get
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        if(!Validate::is($id,'number')){
            return  $this->create([],'id参数不合法！',400);
        }
        $data = AdminModel::withOutField('password')->find($id);
        if(empty($data)){
            return  $this->create([],'无数据',204);
        }else{
            return  $this->create($data,'数据请求成功');
        }
    }

    /**
     * 保存更新的资源
     * put
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $param = $request->param();
        if(!Validate::is($id,'number')){
            return  $this->create([],'id参数不合法！',400);
        }
        try {
            Validate(AdminValidate::class)->remove(['account'=>'require','password'=>'require'])->check($param);
        }catch (ValidateException $validateException){
            return $this->create([],$validateException->getError(),400);
        }
        $admin_model = AdminModel::find($id);
        if(!$admin_model) return  $this->create([],'修改失败,管理员不存在',400);


        if(!empty($param['password'])){
            $update_field = ['password','role_id','nickname','email','status'];
            if(1==$id){
                $update_field = ['password','nickname','email'];
            }
            $param['password'] = md5(sha1($param['password']));
            $edit_result =$admin_model->allowField($update_field)->save($param);
        }else{
            $update_field = ['role_id','nickname','email','status'];
            if(1==$id){
                $update_field = ['nickname','email'];
            }
            $edit_result =$admin_model->allowField($update_field)->save($param);
        }

        if($edit_result){
            return  $this->create($id,'修改成功');
        }else{
            return  $this->create([],'修改失败',400);
        }
    }

    /**
     * 修改状态
     * @param int $id
     * @param int $state
     * @return \think\Response
     */
    public function state($id,$state){
        if(!Validate::is($id,'number') || !Validate::in($state,[0,1])){
            return  $this->create([],'参数不合法！',400);
        }
        if(1==$id){
            return  $this->create([],'设置状态失败，超级管理员不能禁用',403);
        }


        $admin_model = AdminModel::find($id);
        if(!$admin_model) return  $this->create([],'设置状态失败,管理员不存在',400);

        if($admin_model->save(['status'=>$state])){
            return  $this->create($id,'设置状态成功');
        }else{
            return  $this->create([],'设置状态失败',400);
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if(!Validate::is($id,'number')){
            return  $this->create([],'id参数不合法！',400);
        }
        if($id == 1 ) return  $this->create([],'不能删除超级管理员！',403);
        try{
            AdminModel::find($id)->delete();
            return  $this->create([],'删除成功！');
        }catch (\Error $error){
            return  $this->create([],'错误或无数据！',400);
        }
    }
}
